﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.ComponentModel;

using Atosenet.PluginEngine;


namespace Atosenet.IO
{
    [PluginAttribute("Atosenet", "Atosenet.IO.ComIOParameter", "串口端配置", PluginType.IOParameter)]
    public class ComIOParameter : IOParameter
    {
        #region 成员

        public new const string CATEGORY_NAME = "UART端口设置";
        public static ComIOParameter Default = new ComIOParameter("COM1", BaudRateType.BR9600);

        private string _portName = "COM1";
        private BaudRateType _baudRate = BaudRateType.BR9600;
        private int _dataBits = 8;
        private Parity _parity = Parity.None;
        private Handshake _flowControl = Handshake.None;
        private StopBits _stopBits = StopBits.One;

        #endregion


        #region 初始化

        public ComIOParameter()
            : this("COM1", BaudRateType.BR9600)
        { }

        public ComIOParameter(string portName, BaudRateType baudRate)
        {
            _portName = portName;
            _baudRate = baudRate;
        }

        public ComIOParameter(string portName, BaudRateType baudRate, Parity parity, Handshake flowControl, int dataBits, StopBits stopBits)
        {
            _portName = portName;
            _baudRate = baudRate;
            _parity = parity;
            _flowControl = flowControl;
            _dataBits = dataBits;
            _stopBits = stopBits;
        }

        #endregion


        #region 静态函数

        public static string[] GetAvailablePorts()
        {
            return SerialPort.GetPortNames();
        }

        public static bool IsPortAvailable(string portName)
        {
            string[] ports = GetAvailablePorts();

            foreach (string port in ports)
            {
                if (port.ToUpper() == portName.ToUpper())
                    return true;
            }
            return false;
        }

        #endregion


        #region 属性

        [CategoryAttribute(CATEGORY_NAME)]      
        [DescriptionAttribute("串口端口名")]
        public string PortName
        {
            get { return _portName; }
            set
            {
                if (_portName != value)
                {
                    _portName = value;
                    OnParameterChanged();
                }
            }
        }

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("波特率")]
        public BaudRateType BaudRate
        {
            get { return _baudRate; }
            set
            {
                if (_baudRate != value)
                {
                    _baudRate = value;
                    OnParameterChanged();
                }
            }
        }

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("奇偶校验")]
        public Parity PortParity
        {
            get { return _parity; }
            set
            {
                if (_parity != value)
                {
                    _parity = value;
                    OnParameterChanged();
                }
            }
        }

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("流控制协议")]
        public Handshake FlowControl
        {
            get { return _flowControl; }
            set
            {
                if (_flowControl != value)
                {
                    _flowControl = value;
                    OnParameterChanged();
                }
            }
        }

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("数据位")]
        public int DataBits
        {
            get { return _dataBits; }
            set
            {
                if (_dataBits != value)
                {
                    _dataBits = value;
                    OnParameterChanged();
                }
            }
        }

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("停止位")]
        public StopBits StopBits
        {
            get { return _stopBits; }
            set
            {
                if (_stopBits != value)
                {
                    _stopBits = value;
                    OnParameterChanged();
                }
            }
        }

        [DescriptionAttribute("IO名称")]
        public override string IOName
        {
            get { return "串口连接"; }
        }

        #endregion
    }

    public enum BaudRateType
    {
        BR1200 = 1200,
        BR2400 = 2400,
        BR4800 = 4800,
        BR9600 = 9600,
        BR14400 = 14400,
        BR19200 = 19200,
        BR28800 = 28800,
        BR38400 = 38400,
        BR57600 = 57600,
        BR76800 = 76800,
        BR115200 = 115200
    }
}
